/* Copyright (c) 2009, Michael Patraw
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * The name of Michael Patraw may not be used to endorse or promote
 *       products derived from this software without specific prior written
 *       permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Michael Patraw ''AS IS'' AND ANY
 * ExPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Michael Patraw BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, ExEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "at.h"

#include <assert.h>
#include <SDL/SDL.h>

/******************************************************************************\

ATLIB DEFINES

\******************************************************************************/

/* Compatibility with C++.
 */
#ifdef __cplusplus
#define CAST(type) (type)
#else
#define CAST(type)
#endif

/* Translates coordinates from one cell size to another.
 */
#define AT_TRANSLATE(x, y, cx, cy, cw, ch) \
do { \
    x *= cx; y *= cy; \
    x /= cw; y /= ch; \
} while (0)

#define AT_COLOR_TO_SDL(c) \
    SDL_MapRGB(SDL_GetVideoSurface()->format, (c).r, (c).g, (c).b)

/* SDL flags and parameters.
 */
#define AT_BPP 32
#define AT_SDL_FLAGS SDL_SWSURFACE | SDL_DOUBLEBUF

/* Some constants.
 */
#define AT_COLORTABLESIZE 16

#define AT_GLYPH_MAX     128
#define AT_GLYPH_HEIGHT   13
#define AT_GLYPH_WIDTH     6

/* Macros for getting/setting pixels, only works with 32-bit.
 */
#define GET_PIXEL(s, x, y) ((Uint32 *)(s)->pixels)[(y) * (s)->w + (x)]
#define SET_PIXEL(s, x, y, c) ((Uint32 *)(s)->pixels)[(y) * (s)->w + (x)] = (c)

/******************************************************************************\

ATLIB TYPES

\******************************************************************************/

struct atSurface_T {
    SDL_Surface *sdl;
    atColor_T bgcolor;
};

struct atGlyphs_T {
    int gw;
    int gh;
    int capacity;
    int scale;
    int *glyph;
};

/******************************************************************************\

ATLIB CONSTANTS

\******************************************************************************/

const atColor_T ATC_STDBLACK = {0x00, 0x00, 0x00};
const atColor_T ATC_STDBLUE = {0x00, 0x00, 0xFF};
const atColor_T ATC_STDCYAN = {0x00, 0xFF, 0xFF};
const atColor_T ATC_STDGREEN = {0x00, 0xFF, 0x00};
const atColor_T ATC_STDMAGENTA = {0xFF, 0x00, 0xFF};
const atColor_T ATC_STDRED = {0xFF, 0x00, 0x00};
const atColor_T ATC_STDWHITE = {0xFF, 0xFF, 0xFF};
const atColor_T ATC_STDYELLOW = {0xFF, 0xFF, 0x00};

const atColor_T ATC_ALICEBLUE = {0xF0, 0xF8, 0xFF};
const atColor_T ATC_ANTIQUEWHITE = {0xFA, 0xEB, 0xD7};
const atColor_T ATC_AQUA = {0x00, 0xFF, 0xFF};
const atColor_T ATC_AQUAMARINE = {0x7F, 0xFF, 0xD4};
const atColor_T ATC_AZURE = {0xF0, 0xFF, 0xFF};
const atColor_T ATC_BEIGE = {0xF5, 0xF5, 0xDC};
const atColor_T ATC_BISQUE = {0xFF, 0xE4, 0xC4};
const atColor_T ATC_BLACK = {0x00, 0x00, 0x00};
const atColor_T ATC_BLANCHEDALMOND = {0xFF, 0xEB, 0xCD};
const atColor_T ATC_BLUE = {0x00, 0x00, 0xFF};
const atColor_T ATC_BLUEVIOLET = {0x8A, 0x2B, 0xE2};
const atColor_T ATC_BROWN = {0xA5, 0x2A, 0x2A};
const atColor_T ATC_BURLYWOOD = {0xDE, 0xB8, 0x87};
const atColor_T ATC_CADETBLUE = {0x5F, 0x9E, 0xA0};
const atColor_T ATC_CHARTREUSE = {0x7F, 0xFF, 0x00};
const atColor_T ATC_CHOCOLATE = {0xD2, 0x69, 0x1E};
const atColor_T ATC_CORAL = {0xFF, 0x7F, 0x50};
const atColor_T ATC_CORNFLOWERBLUE = {0x64, 0x95, 0xED};
const atColor_T ATC_CORNSILK = {0xFF, 0xF8, 0xDC};
const atColor_T ATC_CRIMSON = {0xDC, 0x14, 0x3C};
const atColor_T ATC_CYAN = {0x00, 0xFF, 0xFF};
const atColor_T ATC_DARKBLUE = {0x00, 0x00, 0x8B};
const atColor_T ATC_DARKCYAN = {0x00, 0x8B, 0x8B};
const atColor_T ATC_DARKGOLDENROD = {0xB8, 0x86, 0x0B};
const atColor_T ATC_DARKGRAY = {0xA9, 0xA9, 0xA9};
const atColor_T ATC_DARKGREY = {0xA9, 0xA9, 0xA9};
const atColor_T ATC_DARKGREEN = {0x00, 0x64, 0x00};
const atColor_T ATC_DARKKHAKI = {0xBD, 0xB7, 0x6B};
const atColor_T ATC_DARKMAGENTA = {0x8B, 0x00, 0x8B};
const atColor_T ATC_DARKOLIVEGREEN = {0x55, 0x6B, 0x2F};
const atColor_T ATC_DARKORANGE = {0xFF, 0x8C, 0x00};
const atColor_T ATC_DARKORCHID = {0x99, 0x32, 0xCC};
const atColor_T ATC_DARKRED = {0x8B, 0x00, 0x00};
const atColor_T ATC_DARKSALMON = {0xE9, 0x96, 0x7A};
const atColor_T ATC_DARKSEAGREEN = {0x8F, 0xBC, 0x8F};
const atColor_T ATC_DARKSLATEBLUE = {0x48, 0x3D, 0x8B};
const atColor_T ATC_DARKSLATEGRAY = {0x2F, 0x4F, 0x4F};
const atColor_T ATC_DARKSLATEGREY = {0x2F, 0x4F, 0x4F};
const atColor_T ATC_DARKTURQUOISE = {0x00, 0xCE, 0xD1};
const atColor_T ATC_DARKVIOLET = {0x94, 0x00, 0xD3};
const atColor_T ATC_DEEPPINK = {0xFF, 0x14, 0x93};
const atColor_T ATC_DEEPSKYBLUE = {0x0, 0x0BF, 0xFF};
const atColor_T ATC_DIMGRAY = {0x69, 0x69, 0x69};
const atColor_T ATC_DIMGREY = {0x69, 0x69, 0x69};
const atColor_T ATC_DODGERBLUE = {0x1E, 0x90, 0xFF};
const atColor_T ATC_FIREBRICK = {0xB2, 0x22, 0x22};
const atColor_T ATC_FLORALWHITE = {0xFF, 0xFA, 0xF0};
const atColor_T ATC_FORESTGREEN = {0x22, 0x8B, 0x22};
const atColor_T ATC_FUCHSIA = {0xFF, 0x00, 0xFF};
const atColor_T ATC_GAINSBORO = {0xDC, 0xDC, 0xDC};
const atColor_T ATC_GHOSTWHITE = {0xF8, 0xF8, 0xFF};
const atColor_T ATC_GOLD = {0xFF, 0xD7, 0x00};
const atColor_T ATC_GOLDENROD = {0xDA, 0xA5, 0x20};
const atColor_T ATC_GRAY = {0x80, 0x80, 0x80};
const atColor_T ATC_GREY = {0x80, 0x80, 0x80};
const atColor_T ATC_GREEN = {0x00, 0x80, 0x00};
const atColor_T ATC_GREENYELLOW = {0xAD, 0xFF, 0x2F};
const atColor_T ATC_HONEYDEW = {0xF0, 0xFF, 0xF0};
const atColor_T ATC_HOTPINK = {0xFF, 0x69, 0xB4};
const atColor_T ATC_INDIANRED = {0xCD, 0x5C, 0x5C};
const atColor_T ATC_INDIGO = {0x4B, 0x00, 0x82};
const atColor_T ATC_IVORY = {0xFF, 0xFF, 0xF0};
const atColor_T ATC_KHAKI = {0xF0, 0xE6, 0x8C};
const atColor_T ATC_LAVENDER = {0xE6, 0xE6, 0xFA};
const atColor_T ATC_LAVENDERBLUSH = {0xFF, 0xF0, 0xF5};
const atColor_T ATC_LAWNGREEN = {0x7C, 0xFC, 0x00};
const atColor_T ATC_LEMONCHIFFON = {0xFF, 0xFA, 0xCD};
const atColor_T ATC_LIGHTBLUE = {0xAD, 0xD8, 0xE6};
const atColor_T ATC_LIGHTCORAL = {0xF0, 0x80, 0x80};
const atColor_T ATC_LIGHTCYAN = {0xE0, 0xFF, 0xFF};
const atColor_T ATC_LIGHTGOLDENRODYELLOW = {0xFA, 0xFA, 0xD2};
const atColor_T ATC_LIGHTGRAY = {0xD3, 0xD3, 0xD3};
const atColor_T ATC_LIGHTGREY = {0xD3, 0xD3, 0xD3};
const atColor_T ATC_LIGHTGREEN = {0x90, 0xEE, 0x90};
const atColor_T ATC_LIGHTPINK = {0xFF, 0xB6, 0xC1};
const atColor_T ATC_LIGHTSALMON = {0xFF, 0xA0, 0x7A};
const atColor_T ATC_LIGHTSEAGREEN = {0x20, 0xB2, 0xAA};
const atColor_T ATC_LIGHTSKYBLUE = {0x87, 0xCE, 0xFA};
const atColor_T ATC_LIGHTSLATEGRAY = {0x77, 0x88, 0x99};
const atColor_T ATC_LIGHTSLATEGREY = {0x77, 0x88, 0x99};
const atColor_T ATC_LIGHTSTEELBLUE = {0xB0, 0xC4, 0xDE};
const atColor_T ATC_LIGHTYELLOW = {0xFF, 0xFF, 0xE0};
const atColor_T ATC_LIME = {0x00, 0xFF, 0x00};
const atColor_T ATC_LIMEGREEN = {0x32, 0xCD, 0x32};
const atColor_T ATC_LINEN = {0xFA, 0xF0, 0xE6};
const atColor_T ATC_MAGENTA = {0xFF, 0x00, 0xFF};
const atColor_T ATC_MAROON = {0x80, 0x00, 0x00};
const atColor_T ATC_MEDIUMAQUAMARINE = {0x66, 0xCD, 0xAA};
const atColor_T ATC_MEDIUMBLUE = {0x00, 0x00, 0xCD};
const atColor_T ATC_MEDIUMORCHID = {0xBA, 0x55, 0xD3};
const atColor_T ATC_MEDIUMPURPLE = {0x93, 0x70, 0xD8};
const atColor_T ATC_MEDIUMSEAGREEN = {0x3C, 0xB3, 0x71};
const atColor_T ATC_MEDIUMSLATEBLUE = {0x7B, 0x68, 0xEE};
const atColor_T ATC_MEDIUMSPRINGGREEN = {0x00, 0xFA, 0x9A};
const atColor_T ATC_MEDIUMTURQUOISE = {0x48, 0xD1, 0xCC};
const atColor_T ATC_MEDIUMVIOLETRED = {0xC7, 0x15, 0x85};
const atColor_T ATC_MIDNIGHTBLUE = {0x19, 0x19, 0x70};
const atColor_T ATC_MINTCREAM = {0xF5, 0xFF, 0xFA};
const atColor_T ATC_MISTYROSE = {0xFF, 0xE4, 0xE1};
const atColor_T ATC_MOCCASIN = {0xFF, 0xE4, 0xB5};
const atColor_T ATC_NAVAJOWHITE = {0xFF, 0xDE, 0xAD};
const atColor_T ATC_NAVY = {0x00, 0x00, 0x80};
const atColor_T ATC_OLDLACE = {0xFD, 0xF5, 0xE6};
const atColor_T ATC_OLIVE = {0x80, 0x80, 0x00};
const atColor_T ATC_OLIVEDRAB = {0x6B, 0x8E, 0x23};
const atColor_T ATC_ORANGE = {0xFF, 0xA5, 0x00};
const atColor_T ATC_ORANGERED = {0xFF, 0x45, 0x00};
const atColor_T ATC_ORCHID = {0xDA, 0x70, 0xD6};
const atColor_T ATC_PALEGOLDENROD = {0xEE, 0xE8, 0xAA};
const atColor_T ATC_PALEGREEN = {0x98, 0xFB, 0x98};
const atColor_T ATC_PALETURQUOISE = {0xAF, 0xEE, 0xEE};
const atColor_T ATC_PALEVIOLETRED = {0xD8, 0x70, 0x93};
const atColor_T ATC_PAPAYAWHIP = {0xFF, 0xEF, 0xD5};
const atColor_T ATC_PEACHPUFF = {0xFF, 0xDA, 0xB9};
const atColor_T ATC_PERU = {0xCD, 0x85, 0x3F};
const atColor_T ATC_PINK = {0xFF, 0xC0, 0xCB};
const atColor_T ATC_PLUM = {0xDD, 0xA0, 0xDD};
const atColor_T ATC_POWDERBLUE = {0xB0, 0xE0, 0xE6};
const atColor_T ATC_PURPLE = {0x80, 0x00, 0x80};
const atColor_T ATC_RED = {0xFF, 0x00, 0x00};
const atColor_T ATC_ROSYBROWN = {0xBC, 0x8F, 0x8F};
const atColor_T ATC_ROYALBLUE = {0x41, 0x69, 0xE1};
const atColor_T ATC_SADDLEBROWN = {0x8B, 0x45, 0x13};
const atColor_T ATC_SALMON = {0xFA, 0x80, 0x72};
const atColor_T ATC_SANDYBROWN = {0xF4, 0xA4, 0x60};
const atColor_T ATC_SEAGREEN = {0x2E, 0x8B, 0x57};
const atColor_T ATC_SEASHELL = {0xFF, 0xF5, 0xEE};
const atColor_T ATC_SIENNA = {0xA0, 0x52, 0x2D};
const atColor_T ATC_SILVER = {0xC0, 0xC0, 0xC0};
const atColor_T ATC_SKYBLUE = {0x87, 0xCE, 0xEB};
const atColor_T ATC_SLATEBLUE = {0x6A, 0x5A, 0xCD};
const atColor_T ATC_SLATEGRAY = {0x70, 0x80, 0x90};
const atColor_T ATC_SLATEGREY = {0x70, 0x80, 0x90};
const atColor_T ATC_SNOW = {0xFF, 0xFA, 0xFA};
const atColor_T ATC_SPRINGGREEN = {0x00, 0xFF, 0x7F};
const atColor_T ATC_STEELBLUE = {0x46, 0x82, 0xB4};
const atColor_T ATC_TAN = {0xD2, 0xB4, 0x8C};
const atColor_T ATC_TEAL = {0x00, 0x80, 0x80};
const atColor_T ATC_THISTLE = {0xD8, 0xBF, 0xD8};
const atColor_T ATC_TOMATO = {0xFF, 0x63, 0x47};
const atColor_T ATC_TURQUOISE = {0x40, 0xE0, 0xD0};
const atColor_T ATC_VIOLET = {0xEE, 0x82, 0xEE};
const atColor_T ATC_WHEAT = {0xF5, 0xDE, 0xB3};
const atColor_T ATC_WHITE = {0xFF, 0xFF, 0xFF};
const atColor_T ATC_WHITESMOKE = {0xF5, 0xF5, 0xF5};
const atColor_T ATC_YELLOW = {0xFF, 0xFF, 0x00};
const atColor_T ATC_YELLOWGREEN = {0x9A, 0xCD, 0x32};

/* Used for easily filling unused spots. */
#define reserved {{1,1,1,1,1,1},\
{1,1,1,1,1,1},\
{1,1,1,1,1,1},\
{1,1,1,1,1,1},\
{1,1,1,1,1,1},\
{1,1,1,1,1,1},\
{1,1,1,1,1,1},\
{1,1,1,1,1,1},\
{1,1,1,1,1,1},\
{1,1,1,1,1,1},\
{1,1,1,1,1,1},\
{1,1,1,1,1,1},\
{1,1,1,1,1,1}}

/* The internal font, 0 is foreground and 1 is background.
 */
static const int _atFont[AT_GLYPH_MAX][AT_GLYPH_HEIGHT][AT_GLYPH_WIDTH] =
{
    reserved, reserved, reserved, reserved, reserved,
    reserved, reserved, reserved, reserved, reserved,
    reserved, reserved, reserved, reserved, reserved,
    reserved, reserved, reserved, reserved, reserved,
    reserved, reserved, reserved, reserved, reserved,
    reserved, reserved, reserved, reserved, reserved,
    reserved, reserved, /* 1-30 */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 31 = ' ' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,1,1,1,1},
     {1,1,0,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 33 = '!' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,1,0,1,1},
     {1,0,1,0,1,1},
     {1,0,1,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 34 = '"' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,1,0,1,1},
     {1,0,1,0,1,1},
     {0,0,0,0,0,1},
     {1,0,1,0,1,1},
     {0,0,0,0,0,1},
     {1,0,1,0,1,1},
     {1,0,1,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 35 = '#' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,0,1,1,1},
     {1,0,0,0,0,1},
     {0,1,0,1,1,1},
     {0,1,0,1,1,1},
     {1,0,0,0,1,1},
     {1,1,0,1,0,1},
     {1,1,0,1,0,1},
     {0,0,0,0,1,1},
     {1,1,0,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 36 = '$' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,1,1,0,1},
     {0,1,0,1,0,1},
     {1,0,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,0,1,1,1},
     {1,0,1,1,1,1},
     {1,0,1,0,1,1},
     {0,1,0,1,0,1},
     {0,1,1,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 37 = '%' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,1,1,1,1},
     {0,1,0,1,1,1},
     {0,1,0,1,1,1},
     {1,0,1,1,1,1},
     {0,1,0,1,1,1},
     {0,1,1,0,0,1},
     {0,1,1,0,1,1},
     {1,0,0,1,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 38 = '&' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 39 = ''' */
    {{1,1,1,1,1,1},
     {1,1,1,0,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,1,0,1,1},
     {1,1,1,1,1,1}}, /* 41 = '(' */
    {{1,1,1,1,1,1},
     {1,0,1,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,0,1,1,1,1},
     {1,1,1,1,1,1}}, /* 40 = ')' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,0,1,1,1},
     {0,1,0,1,0,1},
     {1,0,0,0,1,1},
     {0,1,0,1,0,1},
     {1,1,0,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 41 = '*' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {0,0,0,0,0,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 43 = '+' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,0,0,1,1},
     {1,1,0,1,1,1},
     {1,0,1,1,1,1},
     {1,1,1,1,1,1}}, /* 44 = ',' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,0,0,0,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 45 = '-' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,0,1,1,1},
     {1,0,0,0,1,1},
     {1,1,0,1,1,1},
     {1,1,1,1,1,1}}, /* 46 = '.' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,0,1},
     {1,1,1,1,0,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,0,1,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 47 = '/' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,0,1,1,1},
     {1,0,1,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,0,1,0,1,1},
     {1,1,0,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 48 = '1' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,0,1,1,1},
     {1,0,0,1,1,1},
     {0,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {0,0,0,0,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 49 = '0' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,0,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,1,1,1,0,1},
     {1,1,1,0,1,1},
     {1,1,0,1,1,1},
     {1,0,1,1,1,1},
     {0,1,1,1,1,1},
     {0,0,0,0,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 51 = '1' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,0,0,0,0,1},
     {1,1,1,1,0,1},
     {1,1,1,0,1,1},
     {1,1,0,1,1,1},
     {1,0,0,0,1,1},
     {1,1,1,1,0,1},
     {1,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,0,0,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 50 = '3' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,0,0,1,1},
     {1,0,1,0,1,1},
     {1,0,1,0,1,1},
     {0,1,1,0,1,1},
     {0,0,0,0,0,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 51 = '4' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,0,0,0,0,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,0,0,1,1},
     {0,0,1,1,0,1},
     {1,1,1,1,0,1},
     {1,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,0,0,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 53 = '5' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,0,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,0,0,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,0,0,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 54 = '6' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,0,0,0,0,1},
     {1,1,1,1,0,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 55 = '7' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,0,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,0,0,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,0,0,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 56 = '8' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,0,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,0,0,0,0,1},
     {1,1,1,1,0,1},
     {1,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,0,0,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 57 = '9' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,0,1,1,1},
     {1,0,0,0,1,1},
     {1,1,0,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,0,1,1,1},
     {1,0,0,0,1,1},
     {1,1,0,1,1,1},
     {1,1,1,1,1,1}}, /* 58 = ':' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,0,1,1,1},
     {1,0,0,0,1,1},
     {1,1,0,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,0,0,1,1},
     {1,1,0,1,1,1},
     {1,0,1,1,1,1},
     {1,1,1,1,1,1}}, /* 59 = ';' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,0,1},
     {1,1,1,0,1,1},
     {1,1,0,1,1,1},
     {1,0,1,1,1,1},
     {0,1,1,1,1,1},
     {1,0,1,1,1,1},
     {1,1,0,1,1,1},
     {1,1,1,0,1,1},
     {1,1,1,1,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 61 = '<' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,0,0,0,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,0,0,0,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 60 = '=' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,1,1,1,1,1},
     {1,0,1,1,1,1},
     {1,1,0,1,1,1},
     {1,1,1,0,1,1},
     {1,1,1,1,0,1},
     {1,1,1,0,1,1},
     {1,1,0,1,1,1},
     {1,0,1,1,1,1},
     {0,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 61 = '>' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,0,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,1,1,1,0,1},
     {1,1,1,0,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,1,1,1,1},
     {1,1,0,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 63 = '?' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,0,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,0,0,1},
     {0,1,0,1,0,1},
     {0,1,0,1,0,1},
     {0,1,0,0,1,1},
     {0,1,1,1,1,1},
     {1,0,0,0,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 64 = '@' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,0,1,1,1},
     {1,0,1,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,0,0,0,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 65 = 'A' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,0,0,0,1,1},
     {1,0,1,1,0,1},
     {1,0,1,1,0,1},
     {1,0,1,1,0,1},
     {1,0,0,0,1,1},
     {1,0,1,1,0,1},
     {1,0,1,1,0,1},
     {1,0,1,1,0,1},
     {0,0,0,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 66 = 'B' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,0,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,0,1},
     {1,0,0,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 67 = 'C' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,0,0,0,1,1},
     {1,0,1,1,0,1},
     {1,0,1,1,0,1},
     {1,0,1,1,0,1},
     {1,0,1,1,0,1},
     {1,0,1,1,0,1},
     {1,0,1,1,0,1},
     {1,0,1,1,0,1},
     {0,0,0,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 68 = 'D' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,0,0,0,0,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,0,0,0,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,0,0,0,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 69 = 'E' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,0,0,0,0,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,0,0,0,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 71 = 'F' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,0,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,0,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,0,0,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 70 = 'G' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,0,0,0,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 71 = 'H' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,0,0,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,0,0,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 73 = 'I' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,0,0,0,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {0,1,1,0,1,1},
     {1,0,0,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 74 = 'J' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,0,1,1},
     {0,1,0,1,1,1},
     {0,0,1,1,1,1},
     {0,1,0,1,1,1},
     {0,1,1,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 75 = 'K' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,0,0,0,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 76 = 'L' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,0,1,0,0,1},
     {0,1,0,1,0,1},
     {0,1,0,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 77 = 'M' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,1,1,1,0,1},
     {0,0,1,1,0,1},
     {0,0,1,1,0,1},
     {0,1,0,1,0,1},
     {0,1,0,1,0,1},
     {0,1,1,0,0,1},
     {0,1,1,0,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 78 = 'N' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,0,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,0,0,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 79 = 'O' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,0,0,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,0,0,0,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 81 = 'P' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,0,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,0,1,0,1},
     {1,0,0,0,1,1},
     {1,1,1,1,0,1},
     {1,1,1,1,1,1}}, /* 80 = 'Q' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,0,0,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,0,0,0,1,1},
     {0,1,0,1,1,1},
     {0,1,1,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 81 = 'R' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,0,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {1,0,0,0,1,1},
     {1,1,1,1,0,1},
     {1,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,0,0,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 83 = 'S' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,0,0,0,0,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 84 = 'T' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,0,0,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 85 = 'U' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,0,1,0,1,1},
     {1,0,1,0,1,1},
     {1,0,1,0,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 86 = 'V' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,0,1,0,1},
     {0,1,0,1,0,1},
     {0,1,0,1,0,1},
     {0,1,0,1,0,1},
     {1,0,1,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 87 = 'W' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,0,1,0,1,1},
     {1,0,1,0,1,1},
     {1,1,0,1,1,1},
     {1,0,1,0,1,1},
     {1,0,1,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 88 = 'X' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,0,1,0,1,1},
     {1,0,1,0,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 89 = 'Y' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,0,0,0,0,1},
     {1,1,1,1,0,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,0,1,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {0,1,1,1,1,1},
     {0,0,0,0,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 91 = 'Z' */
    {{1,1,1,1,1,1},
     {1,0,0,0,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {1,0,0,0,1,1},
     {1,1,1,1,1,1}}, /* 90 = '[' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {1,1,0,1,1,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,1,1,0,1},
     {1,1,1,1,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 91 = '\' */
    {{1,1,1,1,1,1},
     {1,0,0,0,1,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,0,0,0,1,1},
     {1,1,1,1,1,1}}, /* 94 = ']' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,0,1,1,1},
     {1,0,1,0,1,1},
     {0,1,1,1,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 95 = '^' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,0,0,0,0,1},
     {1,1,1,1,1,1}}, /* 96 = '_' */
    {{1,1,1,1,1,1},
     {1,1,0,1,1,1},
     {1,1,1,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 97 = '`' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,0,0,1,1},
     {1,1,1,1,0,1},
     {1,0,0,0,0,1},
     {0,1,1,1,0,1},
     {0,1,1,0,0,1},
     {1,0,0,1,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 98 = 'a' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,0,0,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,0,0,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 99 = 'b' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,0,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,0,1},
     {1,0,0,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 011 = 'c' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,0,1},
     {1,1,1,1,0,1},
     {1,1,1,1,0,1},
     {1,0,0,0,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,0,0,0,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 010 = 'd' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,0,0,1,1},
     {0,1,1,1,0,1},
     {0,0,0,0,0,1},
     {0,1,1,1,1,1},
     {0,1,1,1,0,1},
     {1,0,0,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 011 = 'e' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,0,0,1,1},
     {1,0,1,1,0,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {0,0,0,0,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 013 = 'f' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,0,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,0,0,0,0,1},
     {1,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,0,0,0,1,1}}, /* 014 = 'g' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,0,0,1,1},
     {0,0,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 015 = 'h' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,0,1,1,1},
     {1,1,1,1,1,1},
     {1,0,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,0,0,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 016 = 'i' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,0,1,1},
     {1,1,1,1,1,1},
     {1,1,0,0,1,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {1,1,1,0,1,1},
     {0,1,1,0,1,1},
     {0,1,1,0,1,1},
     {1,0,0,1,1,1}}, /* 017 = 'j' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,0,1,1},
     {0,1,0,1,1,1},
     {0,0,1,1,1,1},
     {0,1,0,1,1,1},
     {0,1,1,0,1,1},
     {0,1,1,1,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 018 = 'k' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,0,0,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 019 = 'l' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,0,1,0,1,1},
     {0,1,0,1,0,1},
     {0,1,0,1,0,1},
     {0,1,0,1,0,1},
     {0,1,0,1,0,1},
     {0,1,1,1,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 001 = 'm' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,1,0,0,1,1},
     {0,0,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 000 = 'n' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,0,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,0,0,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 001 = 'o' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,0,0,0,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,0,0,0,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1}}, /* 003 = 'p' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,0,0,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,0,0,0,0,1},
     {1,1,1,1,0,1},
     {1,1,1,1,0,1},
     {1,1,1,1,0,1}}, /* 004 = 'q' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,1,0,0,1,1},
     {0,0,1,1,0,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {0,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 005 = 'r' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,0,0,1,1},
     {0,1,1,1,0,1},
     {1,0,0,1,1,1},
     {1,1,1,0,1,1},
     {0,1,1,1,0,1},
     {1,0,0,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 006 = 's' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {0,0,0,0,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,1,1},
     {1,0,1,1,0,1},
     {1,1,0,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 007 = 't' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,0,0,1},
     {1,0,0,1,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 008 = 'u' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,0,1,0,1,1},
     {1,0,1,0,1,1},
     {1,1,0,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 009 = 'v' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,0,1,0,1},
     {0,1,0,1,0,1},
     {0,1,0,1,0,1},
     {1,0,1,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 011 = 'w' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,1,1,1,0,1},
     {1,0,1,0,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,0,1,0,1,1},
     {0,1,1,1,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 010 = 'x' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,1,0,1},
     {0,1,1,0,0,1},
     {1,0,0,1,0,1},
     {1,1,1,1,0,1},
     {0,1,1,1,0,1},
     {1,0,0,0,1,1}}, /* 011 = 'y' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {0,0,0,0,0,1},
     {1,1,1,0,1,1},
     {1,1,0,1,1,1},
     {1,0,1,1,1,1},
     {0,1,1,1,1,1},
     {0,0,0,0,0,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 013 = 'z' */
    {{1,1,1,1,1,1},
     {1,1,1,0,0,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {0,0,1,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,1,0,0,1},
     {1,1,1,1,1,1}}, /* 014 = '{' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 015 = '|' */
    {{1,1,1,1,1,1},
     {0,0,1,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,1,0,0,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {1,1,0,1,1,1},
     {0,0,1,1,1,1},
     {1,1,1,1,1,1}}, /* 016 = '}' */
    {{1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,0,1,1,0,1},
     {0,1,0,1,0,1},
     {0,1,1,0,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1},
     {1,1,1,1,1,1}}, /* 017 = '~' */
};

/******************************************************************************\

ATLIB STATIC VARIABLES

\******************************************************************************/

static atSurface_T *_atMainSurface = NULL;

static int _atCellWidth = 1;
static int _atCellHeight = 1;

static atColor_T _atColorTable[AT_COLORTABLESIZE] = {
    {255, 255, 255}, {0, 0, 0}
};

static atGlyphs_T *_atGlyphs = NULL;

static atBool_T _atKeyPressed = atFalse;
static int _atKey = ATK_NONE;
static int _atKeyMod = 0;

static atBool_T _atMousePressed = atFalse;
static int _atMouseButton = 0;
static int _atMouseX = 0;
static int _atMouseY = 0;
static int _atMouseDX = 0;
static int _atMouseDY = 0;

static atBool_T _atClosed = atFalse;

static atEventCallback_T _atOnMousePress = NULL;
static atEventCallback_T _atOnMouseRelease = NULL;
static atEventCallback_T _atOnMouseMotion = NULL;
static atEventCallback_T _atOnMouseDrag = NULL;
static atEventCallback_T _atOnKeyPress = NULL;
static atEventCallback_T _atOnKeyRelease = NULL;
static atEventCallback_T _atOnQuit = NULL;

/******************************************************************************\

ATLIB ENUMERATIONS

\******************************************************************************/

/******************************************************************************\

ATLIB FUNCTIONS

\******************************************************************************/

void atSetCellSize(int w, int h)
{
    if (w > 0)
        _atCellWidth = w;
    if (h > 0)
        _atCellHeight = h;
}
void atGetCellSize(int *w, int *h)
{
    if (w)
        *w = _atCellWidth;
    if (h)
        *h = _atCellHeight;
}



void atSetColor(int i, atColor_T c)
{
    if (i >= 0 && i < AT_COLORTABLESIZE) {
        _atColorTable[i] = c;
    }
}
void atSwapColor(int i, int j)
{
    atColor_T tmp;
    
    tmp = atGetColor(i);
    
    atSetColor(i, atGetColor(j));
    atSetColor(j, tmp);
}
atColor_T atGetColor(int i)
{
    if (i >= 0 && i < AT_COLORTABLESIZE) {
        return _atColorTable[i];
    }
    
    return ATC_BLACK;
}
void atSetGlyphs(atGlyphs_T *glyphs)
{
    _atGlyphs = glyphs;
}



atBool_T atStart(const char *title, int w, int h)
{
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
        return atFalse;
    
    _atMainSurface = CAST(atSurface_T *)malloc(sizeof(atSurface_T));
    if (_atMainSurface == NULL)
        return atFalse;
    
    AT_TRANSLATE(w, h, _atCellWidth, _atCellHeight, 1, 1);

    _atMainSurface->sdl = SDL_SetVideoMode(w, h, AT_BPP, AT_SDL_FLAGS);
    _atMainSurface->bgcolor = ATC_BLACK;
    
    if (_atMainSurface->sdl == NULL)
        return atFalse;

    SDL_WM_SetCaption(title, NULL);

    return atTrue;
}
void atStop(void)
{
	SDL_Quit();
    free(_atMainSurface);
    _atMainSurface = NULL;
}
unsigned long atTicks(void)
{
    return SDL_GetTicks();
}
void atDelay(unsigned long ms)
{
    SDL_Delay(ms);
}
void atToggleFullScreen(void)
{
    SDL_WM_ToggleFullScreen(_atMainSurface->sdl);
}
void atSetTitle(const char * title)
{
    SDL_WM_SetCaption(title, NULL);
}
void atKill(void)
{
    _atClosed = atTrue;
}
void atRevive(void)
{
    _atClosed = atFalse;
}
atSurface_T *atMainSurface(void)
{
    return _atMainSurface;
}

void atUpdate(void)
{
    SDL_Flip(atMainSurface()->sdl);
}



void atDrawCell(atSurface_T *s, int x, int y)
{
    int xx, yy, cw, ch;
    
    atGetCellSize(&cw, &ch);
    
    AT_TRANSLATE(x, y, cw, ch, 1, 1);
    
    for (xx = x; xx < x + cw; ++xx)
        for (yy = y; yy < y + ch; ++yy)
			if (xx >= 0 && xx < s->sdl->w && yy >= 0 && yy < s->sdl->h)
				SET_PIXEL(s->sdl, xx, yy, AT_COLOR_TO_SDL(atGetColor(AT_FG)));
}
void atDrawGlyph(atSurface_T *s, int x, int y, int colormax, int pos)
{
    int xx, yy, cw, ch;
    int scale;
    int color_index;
    int *glyph;
    atColor_T oldcolor;
    
    if (_atGlyphs) {
        atGetCellSize(&cw, &ch);
        oldcolor = atGetColor(AT_FG);
        glyph = atGetGlyphAt(_atGlyphs, pos);
        
        scale = _atGlyphs->scale;
        
        AT_TRANSLATE(x, y, cw, ch, 1, 1);
        atSetCellSize(1, 1);
        
        for (yy = 0; yy < _atGlyphs->gh * scale; ++yy) {
            for (xx = 0; xx < _atGlyphs->gw * scale; ++xx) {
                color_index = glyph[(yy/scale) * _atGlyphs->gw + (xx/scale)];
                if (color_index <= colormax) {
                    if (color_index == 0)
                        atSetColor(AT_FG, oldcolor);
                    else
                        atSetColor(AT_FG, atGetColor(color_index));
                    atDrawCell(s, x + xx, y + yy);
                }
            }
        }
        
        atSetColor(AT_FG, oldcolor);
        atSetCellSize(cw, ch);
    }
}
void atDrawString(atSurface_T *s, int x, int y, int colormax, const char *str)
{
    int oldcw, oldch, gw, gh;
    
    
    if (_atGlyphs) {
        atGetCellSize(&oldcw, &oldch);
        atGetGlyphSize(_atGlyphs, &gw, &gh);
        
        AT_TRANSLATE(x, y, oldcw, oldch, 1, 1);
        atSetCellSize(1, 1);
    
        for ( ; *str; ++str) {
            atDrawGlyph(s, x, y, colormax, *str);
            x += gw;
        }
        
        atSetCellSize(oldcw, oldch);
    }
}



atGlyphs_T *atNewGlyphs(int capacity, int gw, int gh)
{
    atGlyphs_T *g;
    
    g = CAST(atGlyphs_T *)malloc(sizeof(atGlyphs_T));
    if (g == NULL)
        return NULL;
    
    g->gw = gw;
    g->gh = gh;
    g->capacity = capacity;
    g->scale = 1;
    g->glyph = CAST(int *)malloc(sizeof(*g->glyph) * capacity * gw * gh);
    if (g->glyph == NULL) {
        free(g);
        return NULL;
    }
    
    memset(&g->glyph[0], 0, sizeof(*g->glyph) * capacity * gw * gh);
    
    return g;
}
void atDelGlyphs(atGlyphs_T *g)
{
    free(g->glyph);
    free(g);
}
void atGetGlyphSize(atGlyphs_T *g, int *w, int *h)
{
    if (w)
        *w = g->gw * g->scale;
    if (h)
        *h = g->gh * g->scale;
}
int atGetGlyphCapacity(atGlyphs_T *g)
{
    return g->capacity;
}
void atSetGlyphScale(atGlyphs_T *g, int s)
{
    if (s > 0)
        g->scale = s;
}
int atGetGlyphScale(atGlyphs_T *g)
{
    return g->scale;
}
void atSetGlyphAt(atGlyphs_T *g, int pos, const int *glyph)
{
    memcpy(
        atGetGlyphAt(g, pos),
        glyph,
        sizeof(*g->glyph) * g->gw * g->gh
    );
}
int *atGetGlyphAt(atGlyphs_T *g, int pos)
{
    return &g->glyph[g->gw * g->gh * pos];
}



atGlyphs_T *atDefaultGlyphs(void)
{
    atGlyphs_T *g;
    int i;
    
    g = atNewGlyphs(AT_GLYPH_MAX, AT_GLYPH_WIDTH, AT_GLYPH_HEIGHT);
    
    if (!g)
        return NULL;
    
    for (i = 0; i < AT_GLYPH_MAX; ++i) {
        atSetGlyphAt(g, i, (const int *)&_atFont[i]);
    }
    
    return g;
}



atSurface_T *atNewSurface(int w, int h)
{
    atSurface_T *s;

    s = CAST(atSurface_T *)malloc(sizeof(atSurface_T));

    if (s) {
        s->bgcolor = ATC_STDBLACK;

        /* Convert dimensions. */
        AT_TRANSLATE(w, h, _atCellWidth, _atCellHeight, 1, 1);

        s->sdl = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, AT_BPP, 0, 0, 0, 0);

        if (!s->sdl) {
            atDelSurface(s);
            return NULL;
        }
    }

    return s;
}
atSurface_T *atLoadBMP(const char *filename)
{
    atSurface_T *s;
    SDL_Surface *tmp;

    s = CAST(atSurface_T *)malloc(sizeof(atSurface_T));

    if (s) {
        s->bgcolor = ATC_STDBLACK;
        s->sdl = NULL;

        tmp = SDL_LoadBMP(filename);

        if (tmp) {
            s->sdl = SDL_DisplayFormat(tmp);
            SDL_FreeSurface(tmp);
        }

        if (!s->sdl) {
            atDelSurface(s);
            return NULL;
        }
    }

    return s;
}
void atDelSurface(atSurface_T *s)
{
    SDL_FreeSurface(s->sdl);
    free(s);
}
void atGetSurfaceSize(atSurface_T *s, int *w, int*h)
{
    int rw, rh;
    
    rw = s->sdl->w;
    rh = s->sdl->h;
    
    AT_TRANSLATE(rw, rh, 1, 1, _atCellWidth, _atCellHeight);
    
    if (w)
        *w = rw;
    if (h)
        *h = rh;
}
atColor_T atGetSurfaceBG(atSurface_T *s)
{
    return s->bgcolor;
}
void atSetSurfaceBG(atSurface_T *s, atColor_T c)
{
    s->bgcolor = c;
    
    atClearSurface(s);
}
void atSetSurfaceColorKey(atSurface_T *s, atColor_T c)
{
    Uint32 colorkey;

    colorkey = SDL_MapRGB(s->sdl->format, c.r, c.g, c.b);

    SDL_SetColorKey(s->sdl, SDL_SRCCOLORKEY, colorkey);
}
void atSetSurfaceAlpha(atSurface_T *s, unsigned char val)
{
    SDL_SetAlpha(s->sdl, SDL_SRCALPHA, val);
}
void atBlitSurface(atSurface_T *dst, int x, int y, atSurface_T *src,
    int sx, int sy, int sw, int sh)
{
    SDL_Rect loc;
    SDL_Rect src_area;
    int w, h;
    
    atGetSurfaceSize(src, &w, &h);

    AT_TRANSLATE(x, y, _atCellWidth, _atCellHeight, 1, 1);
    AT_TRANSLATE(sx, sy, _atCellWidth, _atCellHeight, 1, 1);
    AT_TRANSLATE(w, h, _atCellWidth, _atCellHeight, 1, 1);

    /* Set sw and sh to max if they were -1.
       As sw or sh become lower the size is reduced from the max,
       like -1 is height, -2 is height - 1, -3 is height - 2. */
    if (sw <= -1)
        sw = w - sx + (sw + 1);
    if (sh <= -1)
        sh = h - sy + (sh + 1);
    AT_TRANSLATE(sw, sh, _atCellWidth, _atCellHeight, 1, 1);

    loc.x = x;
    loc.y = y;

    src_area.x = sx;
    src_area.y = sy;
    src_area.w = sw;
    src_area.h = sh;

    SDL_BlitSurface(src->sdl, &src_area, dst->sdl, &loc);
}
void atClearSurface(atSurface_T *s)
{
    SDL_FillRect(s->sdl, NULL, AT_COLOR_TO_SDL(s->bgcolor));
}



void atKeyRepeat(atBool_T yesno)
{
    if (yesno)
        SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
    else
        SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
}



void atGrabEvents(void)
{
    SDL_Event ev;
    SDLMod mod;

    mod = SDL_GetModState();
    
    _atKeyMod = 0;
    
    while (SDL_PollEvent(&ev)) {
        switch (ev.type) {
            case SDL_KEYDOWN: _atKeyPressed = atTrue;
                _atKey = ev.key.keysym.sym;
                
                if (_atOnKeyPress)
                    _atOnKeyPress();
                break;
            case SDL_KEYUP: _atKeyPressed = atFalse;
                _atKey = ev.key.keysym.sym;
                
                if (_atOnKeyRelease)
                    _atOnKeyRelease();
                break;
            case SDL_MOUSEMOTION:
                _atMouseX = ev.motion.x;
                _atMouseY = ev.motion.y;
                _atMouseDX = ev.motion.xrel;
                _atMouseDY = ev.motion.yrel;
                
                if (_atOnMouseMotion)
                    _atOnMouseMotion();
                break;
            case SDL_MOUSEBUTTONDOWN: _atMousePressed = atTrue;
                _atMouseButton |= SDL_BUTTON(ev.button.button);
            
                if (_atOnMousePress)
                    _atOnMousePress();
                break;
            case SDL_MOUSEBUTTONUP: _atMousePressed = atFalse;
                _atMouseButton &= ~SDL_BUTTON(ev.button.button);
            
                if (_atOnMouseRelease)
                    _atOnMouseRelease();
                break;
            case SDL_QUIT:
                _atClosed = 1;
                
                if (_atOnQuit)
                    _atOnQuit();
                break;
            default:
                break;
        }
    }
}



atBool_T atKeyPressed(void)
{
    return _atKeyPressed;
}
int atKey(void)
{
    return _atKey;
}
int atKeyMod(void)
{
    return _atKeyMod;
}
atBool_T atMousePressed(void)
{
    return _atMousePressed;
}
int atMouseButton(void)
{
    return _atMouseButton;
}
int atMouseX(void)
{
    return _atMouseX / _atCellWidth;
}
int atMouseY(void)
{
    return _atMouseY / _atCellHeight;
}
int atMouseDX(void)
{
    return _atMouseDX / _atCellWidth;
}
int atMouseDY(void)
{
    return _atMouseDY / _atCellHeight;
}
atBool_T atClosed(void)
{
    return _atClosed;
}



void atSetOnMousePress(atEventCallback_T cb)
{
    _atOnMousePress = cb;
}
void atSetOnMouseRelease(atEventCallback_T cb)
{
    _atOnMouseRelease = cb;
}
void atSetOnMouseMotion(atEventCallback_T cb)
{
    _atOnMouseMotion = cb;
}
void atSetOnMouseDrag(atEventCallback_T cb)
{
    _atOnMouseDrag = cb;
}
void atSetOnKeyPress(atEventCallback_T cb)
{
    _atOnKeyPress = cb;
}
void atSetOnKeyRelease(atEventCallback_T cb)
{
    _atOnKeyRelease = cb;
}
void atSetOnQuit(atEventCallback_T cb)
{
    _atOnQuit = cb;
}

/* 
 */
